﻿<?xml version="1.0" encoding="utf-8" ?>
<TemplateRoot>
    <TemplateItem Name="DataGroup" FilePath="{$TableName}GroupHelper.cs">
        <TemplateItem Name="Header">
            <Content>
                using System;
                using System.Collections.Generic;
                using System.Text;
                using System.Data;
                using MySql.Data.MySqlClient;
                namespace {@Namespace}.{$DbType}.{$SqlType} {
                internal partial class {@DataGroupName}Helper : {@Namespace}.{@DataGroupName} {
            </Content>
        </TemplateItem>
        <TemplateItem Name="SqlCommand" HasRegion="true">
            <Content>
                const string AddStoredCommand = "add{$TableName}";
                const string GetsStoredCommand = "get{$TableName}List";
                const string DelStoredCommand = "del{$TableName}";
                const string GetStoredCommand = "get{$TableName}";
                const string GetByMainPKStroedCommand = "SELECT #{GetAllColumns()} FROM {$TableName}  WHERE {$Pks[0]} in ({@InputPrefix}{$Pks[0]}) ";
            </Content>
        </TemplateItem>
        <TemplateItem Name="PrivateMethod" HasRegion="true">
            <TemplateItem Name="CreateDataGroup">
                <Content>
                    private static {@DataGroupName} CreateDataGroup(IDataReader reader){
                    {@DataGroupName} group = new {@DataGroupName}();
                    #{InitGroup1()}
                    return group;
                    }
                    private static {@DataGroupName} CreateDataGroup(List&lt;MySqlParameter&gt; ps){
                    {@DataGroupName} group = new {@DataGroupName}();
                    #{InitGroup1()}
                    return group;
                    }
                </Content>
            </TemplateItem>
        </TemplateItem>
        <TemplateItem Name="GetMethod" HasRegion="true">
            <TemplateItem Name="GetList">
                <Content>
                    internal override {@DataGroupName}[] Get{@DataGroupName}ListByPK( {$ColumnTypes[0]}#{GetFullColumnType(#{IsValueType(0)})}[] {@MethodArgumentPrefix}{$Columns[0]}List, #{GetArgusForPK()}){
                    if({@MethodArgumentPrefix}{$Columns[0]}List == null) 
                        throw  new DBGroupException("Argument is not valid!");
                    string {@MethodArgumentPrefix}{$Columns[0]} = string.Join(",",{@MethodArgumentPrefix}{$Columns[0]}List);
                    List&lt;MySqlParameter&gt; ps = new List&lt;MySqlParameter&gt;();
                    #{GetAllParameters(get{$TableName}List)}
                    List&lt;{@DataGroupName}&gt; groups = new  List&lt;{@DataGroupName}&gt;();
                    string sqlCmd = string.Format("{0}.{1}",Names._DBName_,GetsStoredCommand);
                    using (MySqlDataReader reader = MySqlHelper.ExecuteReader(SqlConfigHelper.GetConnectionString(DbName, DbInstanceName, "Client"),System.Data.CommandType.StoredProcedure,sqlCmd,ps.ToArray())) {
                    while(reader.Read()){
                    {@DataGroupName} group = CreateDataGroup(reader);
                    FireGottenEvent(this,"Get{@DataGroupName}ListByPK",group);
                    groups.Add(group);
                    }
                    }
                    return groups.ToArray();
                    }
                </Content>
            </TemplateItem>
            <TemplateItem Name="GetTable">
                <Content>
                    internal override DataTable Get{@DataGroupName}TableByPK( {$ColumnTypes[0]}#{GetFullColumnType(#{IsValueType(0)})}[] {@MethodArgumentPrefix}{$Columns[0]}List, #{GetArgusForPK()}){
                    if({@MethodArgumentPrefix}{$Columns[0]}List == null)
                    throw  new DBGroupException("Argument is not valid!");
                    string {@MethodArgumentPrefix}{$Columns[0]} = string.Join(",",{@MethodArgumentPrefix}{$Columns[0]}List);
                    List&lt;MySqlParameter&gt; ps = new List&lt;MySqlParameter&gt;();
                    #{GetAllParameters(get{$TableName}List)}
                    DataSet ds = new DataSet();
                    string sqlCmd = string.Format("{0}.{1}",Names._DBName_,GetsStoredCommand);
                    ds = MySqlHelper.ExecuteDataset(SqlConfigHelper.GetConnectionString(DbName, DbInstanceName, "Client"),System.Data.CommandType.StoredProcedure,sqlCmd,ps.ToArray());
                    if(ds.Table.Count >=1){
                        return ds.Tables[0];
                    }
                    return new DataTable();
                    }
                </Content>
            </TemplateItem>
            <TemplateItem Name="GetLinkedList">
                <Content>
                    internal override LinkedList&lt;{@DataGroupName}&gt; Get{@DataGroupName}LinkedListByPK( {$ColumnTypes[0]}#{GetFullColumnType(#{IsValueType(0)})}[] {@MethodArgumentPrefix}{$Columns[0]}List, #{GetArgusForPK()}){
                    if({@MethodArgumentPrefix}{$Columns[0]}List == null)
                    throw  new DBGroupException("Argument is not valid!");
                    string {@MethodArgumentPrefix}{$Columns[0]} = string.Join(",",{@MethodArgumentPrefix}{$Columns[0]}List);
                    List&lt;MySqlParameter&gt; ps = new List&lt;MySqlParameter&gt;();
                    #{GetAllParameters(get{$TableName}List)}
                    LinkedList&lt;{@DataGroupName}&gt; groups = new  LinkedList&lt;{@DataGroupName}&gt;();
                    string sqlCmd = string.Format("{0}.{1}",Names._DBName_,GetsStoredCommand);
                    using (MySqlDataReader reader = MySqlHelper.ExecuteReader(SqlConfigHelper.GetConnectionString(DbName, DbInstanceName, "Client"),System.Data.CommandType.StoredProcedure,sqlCmd,ps.ToArray())) {
                    LinkedListNode&lt;{@DataGroupName}&gt; first = null; 
                    while(reader.Read()){
                    {@DataGroupName} group = CreateDataGroup(reader);
                    FireGottenEvent(this,"Get{@DataGroupName}>LinkedListByPK",group);
                    if(groups.Count == 0){
                        fitst = groups.AddFirst(group);
                    }else{
                        groups.AddAfter(first,group);
                    }
                    }
                    }
                    return groups;
                    }
                </Content>
            </TemplateItem>
            <TemplateItem Name="Get">
                <Content>
                    internal override {@DataGroupName} Get{@DataGroupName}ByPK( #{GetArgusForPK2()}){
                    if({@MethodArgumentPrefix}{$Columns[0]} == null)
                    throw  new DBGroupException("Argument is not valid!");
                    List&lt;MySqlParameter&gt; ps = new List&lt;MySqlParameter&gt;();
                    #{GetAllParameters(get{$TableName})}
                    string sqlCmd = string.Format("{0}.{1}",Names._DBName_,GetsStoredCommand);
                    MySqlHelper.ExecuteNonQuery(SqlConfigHelper.GetConnectionString(DbName, DbInstanceName, "Client"), CommandType.StoredProcedure, sqlCmd, ps.ToArray());
                    {@DataGroupName} group = CreateDataGroup(ps);
                    FireGottenEvent(this,"Get{@DataGroupName}ByPK",group);
                    return group;
                    }
                </Content>
            </TemplateItem>
            <TemplateItem Name="GetByMainPK">
                <Content>
                internal override {@DataGroupName}[] Get{@DataGroupName}ByMainPK( {$ColumnTypes[0]}#{GetFullColumnType(#{IsValueType(0)})}[] {@MethodArgumentPrefix}{$Columns[0]}List){
                if({@MethodArgumentPrefix}{$Columns[0]}List == null)
                throw  new DBGroupException("Argument is not valid!");
                string {@MethodArgumentPrefix}{$Columns[0]} = string.Join(",",{@MethodArgumentPrefix}{$Columns[0]}List);
                MySqlParameter p = MySqlHelper.MakeParameter("@{@InputPrefix}{$Pks[0]}", MySqlDbType.MediumText, ParameterDirection.Input, {@MethodArgumentPrefix}{$Columns[0]});
                List&lt;{@DataGroupName}&gt; groups = new  List&lt;{@DataGroupName}&gt;();
                    using (MySqlDataReader reader = MySqlHelper.ExecuteReader(SqlConfigHelper.GetConnectionString(DbName, DbInstanceName, "Client"),System.Data.CommandType.StoredProcedure,GetByMainPKStroedCommand,p)) {
                    while(reader.Read()){
                    {@DataGroupName} group = CreateDataGroup(reader);
                    FireGottenEvent(this,"Get{@DataGroupName}ByMainPK",group);
                    groups.Add(group);
                    }
                    }
                    return groups.ToArray();
                    }
                </Content>
            </TemplateItem>
            <TemplateItem Name="GetByMainPKAndDate">
                <Content>
                    internal override  {@DataGroupName}[] Get{@DataGroupName}ListByMainPK({$ColumnTypes[0]}#{GetFullColumnType(#{IsValueType(0)})}[] {@MethodArgumentPrefix}{$Columns[0]}List, DateTime startTime,DateTime endTime) {
                    if({@MethodArgumentPrefix}{$Columns[0]}List == null) { throw new DBGroupException("Argument is not valid!"); }
                    string ids = string.Join(",", theCategoryIdList);
                    List&lt;MySqlParameter&gt; ps = new List&lt;MySqlParameter&gt;();
                    ps.Add(MySqlHelper.MakeParameter("@{@InputPrefix}{$Pks[0]}", MySqlDbType.MediumText, ParameterDirection.Input, ids));
                    ps.Add(MySqlHelper.MakeParameter("@{@InputPrefix}StartTime", MySqlDbType.VarChar, ParameterDirection.Input, startTime.ToString("yyyy-MM-dd")));
                    ps.Add(MySqlHelper.MakeParameter("@{@InputPrefix}EndTime", MySqlDbType.VarChar, ParameterDirection.Input, endTime.ToString("yyyy-MM-dd")));
                    List&lt;{@DataGroupName}&gt; groups = new  List&lt;{@DataGroupName}&gt;();
                    string sqlCmd = string.Format("{0} and  a.EndDate between @{@InputPrefix}StartTime and @{@InputPrefix}EndTime ",GetByMainPKStroedCommand);
                    using (MySqlDataReader reader = MySqlHelper.ExecuteReader(AppConfigInfo.SqlConfig.GetConnectionString(DbName, DbInstanceName, "Client"),System.Data.CommandType.Text,sqlCmd,ps.ToArray())) {
                    while (reader.Read()) {
                    {@DataGroupName} group = CreateDataGroup(reader);
                    FireGottenEvent(this, "Get{@DataGroupName}ByMainPKAndDate", group);
                    groups.Add(group);
                    }
                    }
                    return groups.ToArray();
                    }
                </Content>
            </TemplateItem>
        </TemplateItem>
        <TemplateItem Name="Footer">
            <Content>
                }
                }
            </Content>
        </TemplateItem>
    </TemplateItem>
    <Functions>
        <Item Name="GetAllColumns">
            <Loop param="${cell}" source ="{$Columns}" only-one-line="true" split=",">
                ${cell} AS {@OutputPrefix}${cell}
            </Loop>
        </Item>
        <Item Name="InitGroup1">
            <Loop  source ="{$Columns}" index="${i}" >
                group.{$Columns[${i}]} = ({$ColumnTypes[${i}]}) reader["{@OutputPrefix}{$Columns[${i}]}"];
            </Loop>
        </Item>
        <Item Name="InitGroup2">
            <Loop  source ="{$Columns}" index="${i}">
                group.{$Columns[${i}]} = ({$ColumnTypes[${i}]}) reader["{@OutputPrefix}{$Columns[${i}]}"];
            </Loop>
        </Item>
        <Item Name="GetArgusForPK">
            <Loop  source ="{$Columns}" only-one-line="true" split="," index="${i}" start="1">
                {$ColumnTypes[[${i}]}#{GetFullColumnType(#{IsValueType(${i})})} {@MethodArgumentPrefix}{$Columns[${i}]}
            </Loop>
        </Item>
        <Item Name="GetArgusForPK2">
            <Loop  source ="{$Columns}" only-one-line="true" split="," index="${i}" >
                {$ColumnTypes[[${i}]} {@MethodArgumentPrefix}{$Columns[${i}]}
            </Loop>
        </Item>
        <Item Name="GetFullColumnType">
            <If test="true">
                ?
            </If>
        </Item>
        <Item Name="IsValueType">
            <IsValueType source="{$ColumnTypes}"  />
        </Item>
        <Item Name="GetAllParameters">
            <LoadProc />
            <Loop source="@args,{$ParameterName}" index="${i}" >
                ps.Add(MySqlHelper2.MakeParameter("@{$@args[ParameterName[${i}]]}",MySqlDbType.{$@args[MySqlDbType[${i}]]},ParameterDirection.{$@args[Direction[${i}]]},#{SetValue({$@args[ParameterName[${i}]]},{@InputPrefix},{@MethodArgumentPrefix})},{$@args[Size[${i}]]},{$@args[Scale[${i}]]},{$@args[Precision[${i}]]}));
            </Loop>
        </Item>
        <Item Name="SetValue">
            <GetValueForProcParam source="{$ColumnTypes}"/>
        </Item>
    </Functions>
    <Macros>
        <Item Name="Namespace">CoporMock</Item>
        <Item Name="DataGroupName">{$TableName}Group</Item>
        <Item Name="OutputPrefix">r_</Item>
        <Item Name="InputPrefix">p_</Item>
        <Item Name="MethodArgumentPrefix">the</Item>
        <Item Name="PropertyArgumentPrefix">this.</Item>
        <Item Name="InstanceArgument">g.</Item>
    </Macros>
</TemplateRoot>